package mww.servlet.course;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import mww.common.DaoException;
import mww.common.Page;
import mww.common.ServiceException;
import mww.javabean.entity.Course;
import mww.javabean.service.CourseService;
import mww.javabean.service.CourseServiceImpl;
public class CheckCourseServlet extends HttpServlet {
	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}
	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		CourseService service = new CourseServiceImpl();
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String courseNo=request.getParameter("courseNo");
		String courseName=request.getParameter("courseName");
		Integer studyTime=null;
		if(request.getParameter("studyTime")!=null&&!request.getParameter("studyTime").equals(""))
			studyTime=new Integer(request.getParameter("studyTime"));
		Float credit=null;
		if(request.getParameter("credit")!=null&&!request.getParameter("credit").equals(""))
			credit=new Float(request.getParameter("credit"));
		Integer courseType=null;
		if(request.getParameter("courseType")!=null&&!request.getParameter("courseType").equals(""))
			courseType=new Integer(request.getParameter("courseType"));
		Integer term=null;
		if(request.getParameter("term")!=null&&!request.getParameter("term").equals(""))
			term=new Integer(request.getParameter("term"));
		
		//将筛选查询条件 传回视图层、以保留查询条件
		request.setAttribute("courseNo", courseNo);
		request.setAttribute("courseName", courseName);
		request.setAttribute("studyTime", studyTime);
		request.setAttribute("credit", credit);
		request.setAttribute("courseType", courseType);
		request.setAttribute("term", term);
		
		Course course = new Course(courseNo, courseName, studyTime, credit, courseType, term);
		int curPage=1;
		String temp=request.getParameter("curPage");
		if(temp!=null){
			curPage=Integer.parseInt(request.getParameter("curPage"));
		}
  		try{
  			//根据筛选条件查询当前页的所有课程
	  		Map map = service.findAllCourseByMostCon(course, curPage);
	  		ArrayList list=(ArrayList) map.get("list");
			Page pa=(Page) map.get("pa");
	  		request.setAttribute("curPage", pa.getCurPage());//向显示页传递当前页页码
	  		request.setAttribute("pageCount",pa.getPageCount());//向显示页传递总页数
	  		request.setAttribute("list", list);//向显示页传递结果集
	  		request.getRequestDispatcher("/admin/courseList.jsp").forward(request, response);
  		}catch (ServiceException e) {
			e.printStackTrace();
			request.setAttribute("se", e);
			request.getRequestDispatcher("/serviceException.jsp").forward(request, response);
		} catch (DaoException e) {
			e.printStackTrace();
			request.setAttribute("de", e);
			request.getRequestDispatcher("/daoException.jsp").forward(request, response);
		}
	}

}
